/**
 * 后台主页js
 */
var mgr= {
	startEvent:function(){
		var winHeight=$(window).height();
		$('#contIfr').attr('height',winHeight-54);
		$('#menusUl').css('height',winHeight-54);
		$('#rightDiv').css('height',winHeight-54);
		
		//登录时间
		mgr.setLoginDate();
		setInterval("mgr.setLoginDate()",1000);
		
		typeAndFlag=typeAndFlag.substring(1,typeAndFlag.length-1);
		var typeAndFlags=typeAndFlag.split(',');
		//控制一级菜单 
		for(var i=0;i<typeAndFlags.length;i++){
			var type=typeAndFlags[i].split(';')[0];
			var flag=typeAndFlags[i].split(';')[1];
			if(type==1){
				if(flag){
					$('#menusUl ul').each(function(idx,obj){
						if($(obj).attr('id')==flag){
							$(obj).css('display','');
							$(obj).prev().css('display','');
						}
					});
				}
			}
			if(type==2){
				if(flag){
					$('#menusUl ul li').each(function(idx,obj){
						if($(obj).attr('name')==flag){
							$(obj).css('display','');
						}
					});
				}
			}
		}
		//控制右边滚动条高度情况
		var hasAll=true;
		$('#menusUl').children('ul').each(function(idx,el){
			if($(el).css('display')=='none'){
				hasAll=false;
			}
		});
		if(hasAll){
			$('#contIfr').attr('height',winHeight);
			$('#menusUl').css('height',winHeight);
			$('#rightDiv').css('height',winHeight);
		}
		
		//右边默认显示页面
		var firstUl=null;
		$('#menusUl').children('ul').each(function(idx,el){
			if($(el).css('display')!='none'){
				firstUl=$(el);
				return false;
			}
		});
		var firstLi=null;
		firstUl.children('li').each(function(idx,el){
			if($(el).css('display')!='none'){
				firstLi=$(el);
				$(el).addClass('cur');
				return false;
			}
		});
		var firstName=firstLi.attr('name');
		var url=mgr.getUrl(firstName);
		$('#contIfr').attr("src",url);
	},
	
	bindEvent:function(){
		//左边菜单下拉效果
		$('#menusUl h3').click(function(){
			var ii=$($(this).find('i[name="ii"]'));
			if(ii.hasClass("fa-angle-double-up")){
				ii.removeClass("fa-angle-double-up");
				ii.addClass("fa-angle-double-down");
				$(this).next().slideUp();
			}else{
				ii.addClass("fa-angle-double-up");
				ii.removeClass("fa-angle-double-down");
				$(this).next().slideDown();
			}
			
		});
		
		$('#menusUl ul li').click(function(){
			$('#menusUl ul li').removeClass('cur');
			$(this).addClass('cur');
			
			var name=$(this).attr('name');
			var url=mgr.getUrl(name);
			$('#contIfr').attr("src",url);
		});
		
		//修改密码
		$('#changePwd').on('click',function(){
			art.dialog({
				lock: true,
				content:$('#updatePwdDiv').html(),
			    title: '修改密码',
			    width: 400,
			    top:100,
			    ok: function () {
			    	var opwd=$.trim($('#opwd').val());
			    	var pwd=$.trim($('#pwd').val());
			    	var rpwd=$.trim($('#rpwd').val());
			    	if(opwd==''){
			    		alert('原密码不能为空！');
			    		return false;
			    	}
			    	if(pwd==''){
			    		alert('新密码不能为空！');
			    		return false;
			    	}
			    	if(rpwd==''){
			    		alert('重复密码不能为空！');
			    		return false;
			    	}
			    	if(pwd!=rpwd){
			    		alert('两次密码输入不一致！');
			    		return false;
			    	}
			    	var ret=true;
			    	var url = 'common/LoginCtrl/updateUserPwd';
			    	var params = {
		    			'opwd':opwd,
		    			'pwd':pwd
		    		};
		    		var cb = function(data) {
		    			if (data.resultCode != 1) {
		    				alert(data.msg);
		    				ret=false;
		    			}else{
		    				alert('修改成功！');
		    			}
		    		};
		    		sendApiNoLoadSync(url, params, cb, 'POST');
		    		if(!ret)
		    			return false;
			    },
			    cancel: true
			});
		});

		//退出
		$('#loginOut').on('click',function(){
			var url = 'common/LoginCtrl/loginOut';
			var params = {};
			var cb = function(data) {
				if (data.resultCode != 1) {
					alert(data.msg);
				}else{
					var url=APP_ADDRESS+'login.jsp';
					parent.location.href=url;
				}
			};
			sendApiNoLoad(url, params, cb, 'POST');
		});
	},
	
	getUrl:function(name){
		var url='';
		if(name=="sysUser"){
			url=APP_ADDRESS+'mgr/sysMgr/sysUser.jsp';
		}if(name=="sysRole"){
			url=APP_ADDRESS+'mgr/sysMgr/sysRole.jsp';
		}if(name=="sysModule"){
			url=APP_ADDRESS+'mgr/sysMgr/sysModule.jsp';
		}if(name=="sysResource"){
			url=APP_ADDRESS+'mgr/sysMgr/sysResource.jsp';
		}else if(name=="department"){
			url=APP_ADDRESS+'mgr/department/department.jsp';
		}else if(name=="duties"){
			url=APP_ADDRESS+'mgr/department/duties.jsp';
		}else if(name=="employee"){
			url=APP_ADDRESS+'mgr/department/employee.jsp';
		}else if(name=="attendance"){
			url=APP_ADDRESS+'mgr/department/attendance.jsp';
		}else if(name=="building"){
			url=APP_ADDRESS+'mgr/product/building.jsp';
		}else if(name=="area"){
			url=APP_ADDRESS+'mgr/product/area.jsp';
		}else if(name=="banBasic"){
			url=APP_ADDRESS+'mgr/product/banBasic.jsp';
		}else if(name=="houseType"){
			url=APP_ADDRESS+'mgr/product/houseType.jsp';
		}else if(name=="banProduct"){
			url=APP_ADDRESS+'mgr/product/banProduct.jsp';
		}else if(name=="payWay"){
			url=APP_ADDRESS+'mgr/product/payWay.jsp';
		}else if(name=="payParameter"){
			url=APP_ADDRESS+'mgr/product/payParameter.jsp';
		}else if(name=="banProductSell"){
			url=APP_ADDRESS+'mgr/product/banProductSell.jsp';
		}else if(name=="customer"){
			url=APP_ADDRESS+'mgr/customer/customer.jsp';
		}
		return url;
	},
	
	setLoginDate:function(){
		var date = new Date();
	    var seperator1 = "-";
	    var seperator2 = ":";
	    var month = date.getMonth() + 1;
	    var strDate = date.getDate();
	    if (month >= 1 && month <= 9) {
	        month = "0" + month;
	    }
	    if (strDate >= 0 && strDate <= 9) {
	        strDate = "0" + strDate;
	    }
	    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
	            + " " + date.getHours() + seperator2 + date.getMinutes()
	            + seperator2 + date.getSeconds();
	    $('#loginDate').html(currentdate);
	},
		
	init : function() {
		mgr.startEvent();
		mgr.bindEvent();
	}
};

$(document).ready(function() {
	mgr.init();
});


function showUserInfo(){
	var url = 'common/LoginCtrl/getCurrentUserInfo';
	var params ={};
	var cb = function(data){
		if(data.resultCode==1){
			var ret=data.result;
			for(var i in ret){
				var text=ret[i];
				if(i=='entryTime'||i=='birthday'){
					text=text==null?'':(new Date(text)).format('yyyy-MM-dd');
				}
				$('#com-addForm td[name="'+i+'"]').html(text);
			}
		}
	};
	sendApiNoLoadSync(url, params, cb, 'POST');
	art.dialog({
		content:$('#lookUserInfoDiv').html(),
	    title: '个人信息',
	    width: 500,
	    top:100,
	    cancel: true
	});
}

